﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using cnFederal.FaCode;

namespace cnFederal.Admin
{
    public partial class VideoList : System.Web.UI.Page
    {
        List<Fvideo> _postList;
        List<Fvideo> _selected;
        public VideoList()
        {
           
           
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            bool display = true;
            string ddClicked = "ctl00$MainContent$DropDownListDisplay";
            string ev = "";
            if (Request.Params["__EVENTTARGET"] != null)
                ev = Request.Params["__EVENTTARGET"].ToString();
            bool clicked = false;
            if (!string.IsNullOrWhiteSpace(ev) && ddClicked == ev)
                clicked = true;
            if (Session["EnabledView"] != null)
                display = Convert.ToBoolean(Session["EnabledView"]);

            if (!IsPostBack || clicked)
            {
                //_nationList = new List<Nation>();
                //_selected = new List<Nation>();
                Fvideos.Load(display);
                _postList = Fvideos.GetAllFvideos();
                _selected = _postList;
                Session["videos"] = _postList;

            }
            else
            {
                _postList = (List<Fvideo>)Session["videos"];
                _selected = _postList;

            }
            if (!IsPostBack)
                Load();
            if (Session["selectedIndex"] != null)
            {
                int indeks = Convert.ToInt32(Session["selectedIndex"]);
                ListView2.SelectedIndex = indeks;

            }
            MainTable.Visible = UserIdentity.Autenticated;
        }

        public void Load()
        {
            txtDate.Text = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day;
            DisplayMonth();

        }

        private void DisplaylistByCalendar()
        {
            if (Session["DateSelected"] != null)
            {
                string d = Convert.ToString(Session["DateSelected"]);
                switch (d)
                {
                    case "Year": DisplayYear(); break;
                    case "Month": DisplayMonth(); break;
                    case "Day": DisplayDay(); break;
                }
            }


        }
        private void DisplayYear()
        {
            Session["DateSelected"] = "Year";
            string[] datoString = new string[3];
            if (txtDate.Text != string.Empty)
            {
                if (txtDate.Text.Contains('-'))
                    datoString = txtDate.Text.Split('-');
            }
            if (datoString != null && datoString.Length == 3)
            {
                _selected = _postList;
                DateTime dato = new DateTime(Convert.ToInt32(datoString[0]), Convert.ToInt32(datoString[1]), Convert.ToInt32(datoString[2]));
                ListView2.DataSource = _selected.Where(c => c.DateRegistered.Year == dato.Year).OrderBy(c => c.DateRegistered).ToList();
                ListView2.DataBind();
            }
        }

        private void DisplayMonth()
        {
            Session["DateSelected"] = "Month";
            string[] datoString = new string[3];
            if (txtDate.Text != string.Empty)
            {
                if (txtDate.Text.Contains('-'))
                    datoString = txtDate.Text.Split('-');
            }
            if (datoString != null && datoString.Length == 3)
            {
                _selected = _postList;
                DateTime dato = new DateTime(Convert.ToInt32(datoString[0]), Convert.ToInt32(datoString[1]), Convert.ToInt32(datoString[2]));
                ListView2.DataSource = _selected.Where(c => c.DateRegistered.Month == dato.Month && c.DateRegistered.Year == dato.Year).OrderBy(c => c.DateRegistered).ToList();
                ListView2.DataBind();
            }
        }

        private void DisplayDay()
        {
            Session["DateSelected"] = "Day";
            string[] datoString = new string[3];
            if (txtDate.Text != string.Empty)
            {
                if (txtDate.Text.Contains('-'))
                    datoString = txtDate.Text.Split('-');
            }
            if (datoString != null && datoString.Length == 3)
            {
                _selected = _postList;
                DateTime dato = new DateTime(Convert.ToInt32(datoString[0]), Convert.ToInt32(datoString[1]), Convert.ToInt32(datoString[2]));
                ListView2.DataSource = _selected.Where(c => c.DateRegistered.Date == dato.Date).OrderBy(c => c.DateRegistered).ToList();
                ListView2.DataBind();
            }
        }

        public int GetWeekNumer(DateTime date)
        {
            CultureInfo ciCurr = CultureInfo.CurrentCulture;
            int weekNum = ciCurr.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
            return weekNum;
        }

        protected void pager_click(object sender, EventArgs e)
        {
            int lastpage = this.DataPager1.StartRowIndex;
            DataPager1.SetPageProperties(lastpage, DataPager1.MaximumRows, false);

            ListView2.DataBind();

        }

        protected void DropDownList_antall_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataPager1.PageSize = Int32.Parse(DropDownList_antall.Text);
            DisplaylistByCalendar();
        }
        protected void txtDate_TextChanged(object sender, EventArgs e)
        {

            Session["selectedIndex"] = -1;
            DisplaylistByCalendar();
        }
        protected void RadioButtonDay_CheckedChanged(object sender, EventArgs e)
        {
            DisplayDay();
        }

        protected void RadioButtonMonth_CheckedChanged(object sender, EventArgs e)
        {
            //TextBoxSelectedDate.Visible = true;
            // DropDownList_antall.SelectedIndex = 2;
            DisplayMonth();
            DataPager1.PageSize = Int32.Parse(DropDownList_antall.Text);
        }

        protected void RadioButtonYear_CheckedChanged(object sender, EventArgs e)
        {
            DisplayYear();
            DataPager1.PageSize = Int32.Parse(DropDownList_antall.Text);
        }

        protected void ImageButtonReferesh_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void ListView2_ItemDeleting(Object sender, ListViewDeleteEventArgs e)
        {
            int key = Convert.ToInt32(this.ListView2.DataKeys[e.ItemIndex].Value);
            Fvideos.DeleteItem(key);
        }
        protected void ListView2_SelectedIndexChanged(object sender, ListViewSelectEventArgs e)
        {

            ListView2.SelectedIndex = e.NewSelectedIndex;
            string pId = ListView2.SelectedDataKey.Value.ToString();
            //Response.Write("Id = " + e.NewSelectedIndex);


        }

        protected void ListView2_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            //this.ListView2.EditIndex = e.NewEditIndex;
            //Session["EditedIndex"] = this.ListView2.EditIndex;
            //ListView2.DataBind();
            //Response.Write("Id = " + e.NewEditIndex);
        }

        protected void ListView2_ItemCanceling1(object sender, ListViewCancelEventArgs e)
        {

            //ListView2.SelectedIndex = -1;
            //Session["ClipboardRow"] = null;
            //BindListView();

        }
        protected void PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            ListView2.DataBind();
        }

        protected void DropDownListDisplay_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool enabledview = false;
            switch (DropDownListDisplay.SelectedValue)
            {
                case "0": enabledview = false;
                    break;
                case "1": enabledview = true;
                    break;
            }
            Session["EnabledView"] = enabledview;
            Fvideos.Load( enabledview);
            _postList = Fvideos.GetAllFvideos();
            Session["videos"] = _postList;
            _selected = _postList;
            DisplaylistByCalendar();
        }

    }
}